import { createRouter, createWebHistory } from "vue-router";
import { useStore } from "vuex";
import layout from '@/layout.vue'
const history = createWebHistory();
const router = createRouter({
  history,
  routes: [
    {
      path: "/",
      redirect: "/login",
      meta: {
        name: "login",
      },
      component: () => import("@/views/login/index.vue"),
    },
    {
      path: "/login",
      name: 'login',
      meta: {
        title: '登录',
      },
      component: () => import("@/views/login/index.vue"),
    },
    {
      path: "/home",
      meta: {
        title: '首页',
        icon: 'HomeOutlined',
        type: '1',
        name: 'home',
        selectDown: false,
      },
      component: layout,
      children: [
        {
          path: "index",
          name: "home",
          meta: {
            title: '首页'
          },
          component: () => import("@/views/home/index.vue"),
        },
      ],
    },
    {
      path: "/user",
      meta: {
        title: '用户',
        icon: 'UserOutlined',
        type: '2',
        name: 'user',
        selectDown: false,
      },
      component: () => layout,
      children: [
        {
          path: "index",
          name: "user",
          meta: {
            title: '用户列表'
          },
          component: () => import("@/views/user/index.vue"),
        },
        {
          path: "personal",
          name: "personal",
          meta: {
            title: '个人中心'
          },
          component: () => import("@/views/user/personal.vue"),
        },
        {
          path: "passwordEdit",
          name: "passwordEdit",
          meta: {
            title: '个人中心'
          },
          component: () => import("@/views/user/passwordEdit.vue"),
        },
      ],
    },
    {
      path: "/config",
      meta: {
        title: '配置',
        icon: 'SettingOutlined',
        type: '3',
        name: 'config',
        selectDown: false,
      },
      component: () => layout,
      children: [
        {
          path: "index",
          name: "config",
          meta: {
            title: '配置'
          },
          component: () => import("@/views/config/index.vue"),
        },
      ],
    },
    {
      path: "/datas",
      meta: {
        title: '数据',
        icon: 'DatabaseOutlined',
        type: '4',
        name: 'datas',
        selectDown: false,
      },
      component: () => layout,
      children: [
        {
          path: "index",
          name: "datas",
          meta: {
            title: '数据列表'
          },
          component: () => import("@/views/datas/index.vue"),
        },
      ],
    },
    {
      path: "/map",
      meta: {
        title: '地图',
        icon: 'PieChartOutlined',
        type: '5',
        name: 'map',
        selectDown: false,
      },
      component: () => layout,
      children: [
        {
          path: "index",
          name: "map",
          meta: {
            title: '地图',
          },
          component: () => import("@/views/map/index.vue"),
        },
      ],
    },
    {
      path: "/application",
      redirect: '/application/index',
      meta: {
        name: 'application',
        title: '应用',
        type: '6',
        icon: 'AppstoreOutlined',
        selectDown: false
      },
      component: () => layout,
      children: [
        {
          path: "index",
          name: "application",
          meta: {
            title: '应用',
          },
          component: () => import("@/views/createApplication/index.vue"),
        },
        {
          path: "add",
          name: "applicationAdd",
          meta: {
            title: '应用添加',
          },
          component: () => import("@/views/createApplication/add.vue"),
        },
      ],
    },
    {
      path: "/record",
      redirect: '/record/record',
      name: 'record',
      meta: {
        title: '日志',
        icon: 'ProfileOutlined',
        selectDown: true,
      },
      component: () => layout,
      children: [
        {
          path: "record",
          name: "record",
          meta: {
            render: true,
            type: '7',
            title: '系统日志',
          },
          component: () => import("@/views/record/index.vue"),
        },
        {
          path: "dataRecord",
          name: "dataRecord",
          meta: {
            render: true,
            type: '8',
            title: '数据日志',
          },
          component: () => import("@/views/record/dataRecord.vue"),
        }
      ],
    },
  ],
});

// 路由守卫
router.beforeEach((to, from, next) => {
  const { state, commit } = useStore()
  commit('saveUrl', from.name)
  next()
});

export default router;
